package com.cpedia.android.deal;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Config;
import android.util.Log;

/**
 * User: pchen
 * Date: Jun 4, 2009
 */


public class Preferences {
    private static Preferences preferences;

    SharedPreferences mSharedPreferences;

    private Preferences(Context context) {
        mSharedPreferences = context.getSharedPreferences("DailyDeal.Main", Context.MODE_PRIVATE);
    }


    public static synchronized Preferences getPreferences(Context context) {
        if (preferences == null) {
            preferences = new Preferences(context);
        }
        return preferences;
    }

    public Account[] getAccounts() {
        String accountUuids = mSharedPreferences.getString("accountUuids", null);
        if (accountUuids == null || accountUuids.length() == 0) {
            return new Account[] {};
        }
        String[] uuids = accountUuids.split(",");
        Account[] accounts = new Account[uuids.length];
        for (int i = 0, length = uuids.length; i < length; i++) {
            accounts[i] = new Account(this, uuids[i]);
        }
        return accounts;
    }

    public Account getDefaultAccount() {
        String defaultAccountUuid = mSharedPreferences.getString("defaultAccountUuid", null);
        Account defaultAccount = null;
        Account[] accounts = getAccounts();
        if (defaultAccountUuid != null) {
            for (Account account : accounts) {
                if (account.getUuid().equals(defaultAccountUuid)) {
                    defaultAccount = account;
                    break;
                }
            }
        }

        if (defaultAccount == null) {
            if (accounts.length > 0) {
                defaultAccount = accounts[0];
                setDefaultAccount(defaultAccount);
            }
        }

        return defaultAccount;
    }
    
    public void setDefaultAccount(Account account) {
        mSharedPreferences.edit().putString("defaultAccountUuid", account.getUuid()).commit();
    }

    public void setEnableDebugLogging(boolean value) {
        mSharedPreferences.edit().putBoolean("enableDebugLogging", value).commit();
    }

    public boolean geteEnableDebugLogging() {
        return mSharedPreferences.getBoolean("enableDebugLogging", false);
    }

    public void setEnableSensitiveLogging(boolean value) {
        mSharedPreferences.edit().putBoolean("enableSensitiveLogging", value).commit();
    }

    public boolean getEnableSensitiveLogging() {
        return mSharedPreferences.getBoolean("enableSensitiveLogging", false);
    }

    public void save() {
    }

    public void clear() {
        mSharedPreferences.edit().clear().commit();
    }

    public void dump() {
        if (Config.LOGV) {
            for (String key : mSharedPreferences.getAll().keySet()) {
                Log.v(Deal.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key));
            }
        }
    }
}
